//
//  E7AIFunc.cpp
//  EndlessBattle
//
//  Created by cc on 12-4-15.
//  Copyright (c) 2012年 PalmGame. All rights reserved.
//

#include "E7AIFunc.h"

#include "E7Logic.h"

CE7AIFunc::CE7AIFunc(int robotid)
{
    m_nRobotID = robotid ;

//    m_listAttack.clear() ;
//    m_listMove.clear() ;
}

CE7AIFunc::~CE7AIFunc()
{
}

void CE7AIFunc::RunAI()
{
    while (GetResult_LV1()) ;
}

//sAIData* CE7AIFunc::GetResult()
//{
//    return NULL ;
//}

bool CE7AIFunc::GetResult_LV1()
{
    bool res = false ;

    // get country.
    CE7Country* pCountry = s_pLogic->GetSelectCountryInfo(m_nRobotID) ;
    if (NULL == pCountry)
    {
//        printf("CE7AIFunc::GetResult_LV1 NULL == pCountry\n") ;
        return res ;
    }

    if (0 == pCountry->m_listTownIDs.size())
    {
//        printf("CE7AIFunc::GetResult_LV1 0 == pCountry->m_listTownIDs.size()\n") ;
        return res ;
    }

//    printf("debuginfo ==== [%lu]\n", pCountry->m_listTownIDs.size()) ;
    // for towns.
    TOWN_ID_LIST::iterator itr_townid ;
    CE7Town* pTown ;
    for (itr_townid = pCountry->m_listTownIDs.begin(); itr_townid != pCountry->m_listTownIDs.end(); ++itr_townid)
    {
        pTown = s_pLogic->GetSelectTownInfo(*itr_townid) ;
        if (NULL == pTown)
        {
//            printf("CE7AIFunc::GetResult_LV1 NULL == pTown [%d]\n", *itr_townid) ;
            continue ;
        }

        m_listUnitIDs.clear() ;
        m_listData.clear() ;

        // 1. find all can action units.
        SelectUnitFromTown(pTown) ;

        // 2. add neighbor cells to list: attack, move, attach.
        SelectCellFromTown(pTown) ;

        // 3. can do action list.
        DATA_LIST listCanDo ;

        // 4. check can do action.
        sAIData* pRes = NULL ;
        for (DATA_LIST::iterator itr_data = m_listData.begin(); itr_data != m_listData.end(); ++itr_data)
        {
            // check.
            if (PT_ATTACK == (*itr_data)->pt) // && 0 == (*itr_data)->tarcountryid)
            {
                pRes = *itr_data ;
                break ;
            }
        }
        if (NULL != pRes)
        {
            for (UNIT_ID_LIST::iterator itr = m_listUnitIDs.begin(); itr != m_listUnitIDs.end(); ++itr)
            {
                CE7Unit* pUnit = s_pLogic->GetSelectUnitInfo(*itr) ;
                if (pUnit->GetUV() >= pRes->needuv)
                {
                    return s_pLogic->GetRule()->Attack(pUnit->GetCountryID(), pUnit->GetID(), pRes->tarcellid) ;
//                    break ;
                }
            }

            // 1. compute golds.
            int nowgold = pTown->GetGold() ;
            if (UNIT_NEED_GOLD_ARRAY[pRes->needuv] > (nowgold+pTown->GetInGold()))
            {
                // can not create unit.
            }
            else
            {
                return (-1 < s_pLogic->GetRule()->CreateUnit(m_nRobotID, pRes->townid, pRes->needuv, pRes->tarcellid)) ;
            }
        }
    }

    return res ;
}

void CE7AIFunc::SelectUnitFromTown(CE7Town* pTown)
{
    UNIT_ID_LIST::iterator itr_unitid ;
    CE7Unit* pUnit ;
    for (itr_unitid = pTown->m_listUnitIDs.begin(); itr_unitid != pTown->m_listUnitIDs.end(); ++itr_unitid)
    {
        pUnit = s_pLogic->GetSelectUnitInfo(*itr_unitid) ;
        if (NULL == pUnit)
        {
//            printf("CE7AIFunc::SelectUnitFromTown NULL == pUnit\n") ;
            continue ;
        }
        if (CheckAddedUnit(pUnit->GetID()))
        {
            continue ;
        }
        if (pUnit->IsActioned())
        {
            continue ;
        }

        m_listUnitIDs.push_back(pUnit->GetID()) ;
    }
}

void CE7AIFunc::SelectCellFromTown(CE7Town* pTown)
{
//    printf("enter CE7AIFunc::SelectCellFromTown ==== [%lu]\n", pTown->m_listCellIDs.size()) ;
    CELL_ID_LIST::iterator itr_cellid ;
    CE7Cell* pCell ;
    for (itr_cellid = pTown->m_listCellIDs.begin(); itr_cellid != pTown->m_listCellIDs.end(); ++itr_cellid)
    {
        pCell = s_pLogic->ActiveMap()->GetSelectCellInfo(*itr_cellid) ;
        if (NULL == pCell)
        {
//            printf("CE7AIFunc::SelectCellFromTown NULL == pCell\n") ;
            continue ;
        }
        for (int i = 0; i < 6; ++i)
        {
            CE7Cell* pObjectCell = pCell->GetNeighborPointer(i) ;
            if (NULL == pObjectCell)
            {
//                printf("null neighbor \n") ;
                continue ;
            }

            if (-1 == pObjectCell->GetCountryID())
            {
//                printf(" countryid is -1 [%d]\n", pObjectCell->GetID()) ;
                continue ;
            }

            if (CheckAddedData(pObjectCell->GetID()))
            {
                continue ;
            }

            if (pCell->GetCountryID() != pObjectCell->GetCountryID())
            {
                ComputeAttackValue(pCell->GetCountryID(), pCell->GetTownID(), pObjectCell) ;
            }
            else if (pCell->GetTownID() == pObjectCell->GetTownID())
            {
                if (-1 == pObjectCell->GetUnitID())
                {
                    ComputeMoveValue(pCell->GetCountryID(), pCell->GetTownID(), pObjectCell) ;
                }
                else
                {
                    CE7Unit* pTempUnit = s_pLogic->GetSelectUnitInfo(pObjectCell->GetUnitID()) ;
                    if (NULL == pTempUnit)
                    {
//                        printf("CE7AIFunc::SelectCellFromTown NULL == pTempUnit\n") ;
                        continue ;
                    }
                    if (UV_SOLDIER_1 == pTempUnit->GetUV() || UV_SOLDIER_2 == pTempUnit->GetUV())
                    {
                        ComputeAttachValue(pCell->GetCountryID(), pCell->GetTownID(), pObjectCell) ;
                    }
                }
            }
            else
            {
//                printf("CE7AIFunc::SelectCellFromTown pCell->GetTownID() != pCell->GetNeighborPointer(i)->GetTownID() [%d][%d]\n",
//                       pCell->GetTownID(), pCell->GetNeighborPointer(i)->GetTownID()) ;
            }
        }
    }
//    printf("exit CE7AIFunc::SelectCellFromTown ==== [%lu]\n", pTown->m_listCellIDs.size()) ;
}

void CE7AIFunc::ComputeAttackValue(int countryid, int townid, CE7Cell* pTarCell)
{
    sAIData* pNew = new sAIData() ;
    pNew->townid = townid ;
    pNew->tarcellid = pTarCell->GetID() ;
    pNew->tartownid = pTarCell->GetTownID() ;
    pNew->tarcountryid = pTarCell->GetCountryID() ;
    pNew->pt = PT_ATTACK ;
    pTarCell->UpdateUV() ;

    if (UV_SOLDIER_3 <= pTarCell->GetUV())
    {
        pNew->needuv = UV_SOLDIER_NULL ;
    }
    else if (UV_SOLDIER_2 <= pTarCell->GetUV())
    {
        pNew->needuv = UV_SOLDIER_3 ;
    }
    else if (UV_SOLDIER_1 <= pTarCell->GetUV())
    {
        pNew->needuv = UV_SOLDIER_2 ;
    }
    else
    {
        pNew->needuv = UV_SOLDIER_1 ;
    }

    m_listData.push_back(pNew) ;
//    printf("CE7AIFunc::ComputeAttackValue \n") ;
}

void CE7AIFunc::ComputeMoveValue(int countryid, int townid, CE7Cell* pTarCell)
{
    sAIData* pNew = new sAIData() ;
    pNew->townid = townid ;
    pNew->tarcellid = pTarCell->GetID() ;
    pNew->tartownid = pTarCell->GetTownID() ;
    pNew->tarcountryid = pTarCell->GetCountryID() ;
    pNew->pt = PT_MOVE ;
    pTarCell->UpdateUV() ;
    pNew->needuv = UV_NONE ;
    m_listData.push_back(pNew) ;
//    printf("CE7AIFunc::ComputeMoveValue \n") ;
}

void CE7AIFunc::ComputeAttachValue(int countryid, int townid, CE7Cell* pTarCell)
{
    sAIData* pNew = new sAIData() ;
    pNew->townid = townid ;
    pNew->tarcellid = pTarCell->GetID() ;
    pNew->tartownid = pTarCell->GetTownID() ;
    pNew->tarcountryid = pTarCell->GetCountryID() ;
    pNew->pt = PT_ATTACH ;
    pTarCell->UpdateUV() ;
    pNew->needuv = UV_NONE ;
    m_listData.push_back(pNew) ;
//    printf("CE7AIFunc::ComputeAttachValue \n") ;
}

bool CE7AIFunc::CheckAddedData(int cellid)
{
    for (DATA_LIST::iterator itr = m_listData.begin(); itr != m_listData.end(); ++itr)
    {
        if (cellid == (*itr)->tarcellid)
        {
            return true ;
        }
    }

    return false ;
}

bool CE7AIFunc::CheckAddedUnit(int unitid)
{
    for (UNIT_ID_LIST::iterator itr = m_listUnitIDs.begin(); itr != m_listUnitIDs.end(); ++itr)
    {
        if (unitid == (*itr))
        {
            return true ;
        }
    }

    return false ;
}



















